//
//  GSDKActivityIndicatorView.m
//  GameSDK
//
//  Created by Johnny on 2017/7/28.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKActivityIndicatorView.h"
#import "Masonry.h"
#import "GSDKCommonMethod.h"
#import "UIViewController+Extension.h"
#import "YYKit.h"
static GSDKActivityIndicatorView * shared = nil;
@interface GSDKActivityIndicatorView()
@property (nonatomic, retain) UIButton * backgroundButton;
@property (nonatomic, retain) UIActivityIndicatorView * aiView;
@end

@implementation GSDKActivityIndicatorView
/**
 初始化覆盖加载界面类
 */
+ (GSDKActivityIndicatorView*)shared {
    static dispatch_once_t pred;
    dispatch_once(&pred,^{
        if (shared == nil) {
            shared = [[self alloc] init];
            [shared setupUI];
        }
    });
    return shared;
}

/**
 展示
 */
- (void)show {
    __weak GSDKActivityIndicatorView * bSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        [bSelf.aiView startAnimating];
        bSelf.frame = CGRectMake(0, 0, [GSDKCommonMethod getWidth], [GSDKCommonMethod getHight]);
        [[UIViewController getCurrentRootViewController].view addSubview:bSelf];
    });
}

/**
 隐藏
 */
- (void)hide {
    __weak GSDKActivityIndicatorView * bSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        [bSelf.aiView stopAnimating];
        [bSelf removeFromSuperview];
    });
}

#pragma mark - UI
- (void)setupUI {
    _backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backgroundButton.frame = CGRectMake(-[GSDKCommonMethod getWidth], -[GSDKCommonMethod getHight], [GSDKCommonMethod getWidth]*3, [GSDKCommonMethod getHight]*3);
    _backgroundButton.backgroundColor = [UIColor blackColor];
    _backgroundButton.alpha = 0.3;
    [_backgroundButton addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_backgroundButton];
    
    self.backgroundColor = [UIColor clearColor];
    
    _aiView=[[UIActivityIndicatorView alloc]init];
    [_aiView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [_aiView setBackgroundColor:[UIColor whiteColor]];
    [_aiView setColor:[UIColor colorWithHexString:@"#1c76d0"]];
    _aiView.alpha = 0.8;
    [self addSubview:_aiView];
    [_aiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@80);
        make.center.equalTo(@0);
    }];
    
    _aiView.layer.cornerRadius = 8;
}

#pragma mark - Action
- (void)clickAction {
    
}

@end
